package com.ydsungan.service.impl;

import com.ydsungan.common.DateUtils;
import com.ydsungan.dao.*;
import com.ydsungan.pojo.*;
import com.ydsungan.service.HomeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class HomeServiceImpl implements HomeService {

    @Autowired
    private InformMapper informMapper;

    @Autowired
    private DailyMapper dailyMapper;

    @Autowired
    private VersionMapper versionMapper;

    @Autowired
    private DailyQuestionMapper dailyQuestionMapper;

    @Autowired
    private LikeMapper likeMapper;

    @Autowired
    private CollectMapper collectMapper;

    @Autowired
    private ReviewMapper reviewMapper;

    @Autowired
    private PracticeMapper practiceMapper;

    @Autowired
    private FeedbackMapper feedbackMapper;

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private ChapterMapper chapterMapper;

    @Autowired
    private ShareMapper shareMapper;

    @Override
    public List<Inform> listInform(InformExample informExample) {
        return informMapper.selectByExample(informExample);
    }

    @Override
    public long countByExample(DailyQuestionExample dailyQuestionExample) {
        return dailyQuestionMapper.countByExample(dailyQuestionExample);
    }

    @Override
    public DailyQuestion selectById(Integer id) {
        return dailyQuestionMapper.selectByPrimaryKey(id);
    }

    @Override
    public int insertDaily(Daily daily) {
        return dailyMapper.insert(daily);
    }

    @Override
    public List<Daily> selectDailyByExample(DailyExample dailyExample) {
        return dailyMapper.selectByExample(dailyExample);
    }

    @Override
    public int hasLikedDaily(String openId, Integer dailyId) {
        LikeExample likeExample = new LikeExample();
        LikeExample.Criteria criteria = likeExample.createCriteria();
        criteria.andContentIdEqualTo(dailyId);
        criteria.andUserIdEqualTo(openId);
        criteria.andCategoryEqualTo("daily");
        List<Like> likeList = likeMapper.selectByExample(likeExample);
        if(likeList.size() == 0){
            return 0;
        }else{
            return 1;
        }
    }

    @Override
    public int hasCollectedDaily(String openId, Integer dailyId) {
        CollectExample collectExample = new CollectExample();
        CollectExample.Criteria criteria = collectExample.createCriteria();
        criteria.andCategoryEqualTo("daily");
        criteria.andContentIdEqualTo(dailyId);
        criteria.andUserIdEqualTo(openId);
        List<Collect> collectList = collectMapper.selectByExample(collectExample);
        if(collectList.size() == 0){
            return 0;
        }else{
            return 1;
        }



    }

    @Override
    public int countDailyLikeNum(int contentId) {
        LikeExample likeExample = new LikeExample();
        LikeExample.Criteria criteria = likeExample.createCriteria();
        criteria.andCategoryEqualTo("daily");
        criteria.andContentIdEqualTo(contentId);
        return (int)likeMapper.countByExample(likeExample);
    }

    @Override
    public int countDailyCollectNum(int contentId) {
        CollectExample collectExample = new CollectExample();
        CollectExample.Criteria criteria = collectExample.createCriteria();
        criteria.andCategoryEqualTo("daily");
        criteria.andContentIdEqualTo(contentId);
        return (int)collectMapper.countByExample(collectExample);
    }

    @Override
    public void addDailyLike(String openId, int contentId) {
        Like like = new Like();
        like.setCategory("daily");
        like.setContentId(contentId);
        like.setUserId(openId);

        likeMapper.insert(like);

    }

    @Override
    public void addDailyCollect(String openId, int contentId, String type) {
        Collect collect = new Collect();
        collect.setCategory(type);
        collect.setContentId(contentId);
        collect.setUserId(openId);
        collect.setCreateTime(new Date());
        collectMapper.insert(collect);
    }

    @Override
    public List<Integer> getReviewDaysList(String openId, Integer bookId) throws ParseException {

        int days[] = {1,2,6,30};
        List<Integer> daysList = new ArrayList<>();
        daysList.add(1);
        daysList.add(2);
        daysList.add(6);
        daysList.add(30);
        //以上是固有的

        //如果-1昨天没有复习，那么相对今天就应该复习前2天，前3天，前7天，前31天
        //如果-2天没有复习，那么相对今天就应该复习前3、4、8、32天，
        //如果-3。。。。。。。。。。。。。。。。。。4、5、9、33
        //-4，                                5、6、10、34
        //-5                                 6、7、11、35
        //-6                                 7、8、12、36
        //-7                                 8、9、13、37
        //如果昨天有复习，但是没有复习完呢？？
        for(int i = 1; i <= 7; ++i){
            List<Date> timeList = DateUtils.getDateStartAndEnd(i);
            ReviewExample reviewExample = new ReviewExample();
            ReviewExample.Criteria criteria = reviewExample.createCriteria();
            criteria.andUserIdEqualTo(openId);
            criteria.andBookIdEqualTo(bookId);
            criteria.andCreateTimeBetween(timeList.get(0), timeList.get(1));
            List<Review> reviewList = reviewMapper.selectByExample(reviewExample);
            if(reviewList.size() == 0){
                //说明这一天没有复习
                //把 i + 1， i + 2，i + 6，i + 30，加入daysList
                for(int j = 0; j < days.length; ++j){
                    if(!daysList.contains(i + days[j])){
                        daysList.add(i + days[j]);
                    }
                }

            }
        }
        return daysList;
    }

    @Override
    public int countErrorQuestionNum(String openId, Integer bookId) throws ParseException {

        int total = 0;
        List<Integer> daysList = this.getReviewDaysList(openId, bookId);
        for(int i = 0; i < daysList.size(); ++i){
            List<Date> timeList = DateUtils.getDateStartAndEnd(daysList.get(i));
            PracticeExample practiceExample = new PracticeExample();
            PracticeExample.Criteria criteria = practiceExample.createCriteria();
            criteria.andUserIdEqualTo(openId);
            criteria.andIsRightEqualTo(0);
            //criteria.andStatusEqualTo(1);
            criteria.andBookIdEqualTo(bookId);
            criteria.andCreateTimeBetween(timeList.get(0), timeList.get(1));
            int count = (int)practiceMapper.countByExample(practiceExample);
            total += count;
        }
        return total;
    }

    @Override
    public void feedBack(Feedback feedback) {
        feedbackMapper.insert(feedback);
    }

    @Override
    public List<Version> listVersionRecords() {
        VersionExample versionExample = new VersionExample();
        return versionMapper.selectByExample(versionExample);
    }

    @Override
    public List<CollectVO> listCollect(String openId) {
        CollectExample collectExample = new CollectExample();
        CollectExample.Criteria criteria = collectExample.createCriteria();
        criteria.andUserIdEqualTo(openId);
        List<Collect> collectList = collectMapper.selectByExample(collectExample);
        List<CollectVO> collectVOList = new ArrayList<>();
        for(int i = 0; i < collectList.size(); ++i){
            CollectVO collectVO = new CollectVO();
            BeanUtils.copyProperties(collectList.get(i), collectVO);
            if(collectList.get(i).getCategory().equals("daily")){
                Daily daily = dailyMapper.selectByPrimaryKey(collectList.get(i).getContentId());
                DailyQuestion dailyQuestion = dailyQuestionMapper.selectByPrimaryKey(daily.getQuestionId());
                collectVO.setContent(dailyQuestion.getQuestion()+"#"+dailyQuestion.getAnswer());
            }else{
                Question question = questionMapper.selectByPrimaryKey(collectList.get(i).getContentId());
                Book book = bookMapper.selectByPrimaryKey(question.getBookId());
                Chapter chapter = chapterMapper.selectByPrimaryKey(question.getChapterId());
                String single = question.getMultiple() == 1 ? "多选" : "单选";
                collectVO.setContent(question.getQuestionText()+"#"+book.getAbbr()+"-"+chapter.getAbbr()+"-"+single);
            }
            collectVOList.add(collectVO);
        }
        return collectVOList;
    }

    @Override
    public int insertShareRecord(Share share) {
        return shareMapper.insert(share);
    }

    @Override
    public List<Share> listShare(String openId) {
        ShareExample shareExample = new ShareExample();
        ShareExample.Criteria criteria = shareExample.createCriteria();
        criteria.andUserIdEqualTo(openId);

        return shareMapper.selectByExample(shareExample);
    }


}
